package com.dodayum.dubbo;

import com.alibaba.dubbo.common.URL;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author DoDayum
 * @Date 2024-01-24-22:07
 */

@Service
public class UrlList {

    private static final Map<String, URL> urlMap = new HashMap<>();

    public static Map<String, URL> getAllUrl() {
        return urlMap;
    }

    public static void deleteAllUrl() {
        urlMap.clear();
    }

    /**
     * 根据url获取key
     * @param url url
     * @return 由interface，methods组成得key
     */
    private static String buildKey(URL url) {
        Map<String, String> parameters = url.getParameters();
        String interfaceKey = parameters.get("interface");
        String methodsKey = parameters.get("methods");
        return interfaceKey + "&" + methodsKey;
    }

    /**
     * 添加
     * @param url 需要被添加得url
     */
    public static void addUrl(URL url) {
        String key = buildKey(url);
        urlMap.put(key, url);
    }

    /**
     * 删除
     * @param url 需要被添加得url
     */
    public static void removeUrl(URL url){
        String key = buildKey(url);
        urlMap.remove(key);
    }


}
